package com.yc.system.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.system.entity.Book;
import com.yc.system.entity.BookCartLike;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.system.vo.CartInfo;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 购物车/收藏 Mapper 接口
 * </p>
 *
 * @author 1850140986@qq.com
 * @since 2022-12-03
 */
@Mapper
public interface BookCartLikeMapper extends BaseMapper<BookCartLike> {

    @Select("select * from t_book_cart_like where user_id=#{userId} and book_id=#{bookId} and deleted = 0")
    BookCartLike getBCLByUserId(int userId,int bookId);

    @Update("update t_book_cart_like set cl_status=#{clStatus},cart_num=#{cartNum} where cl_id=#{clId}")
    int updateByclId(BookCartLike model);

    @Select("select * from t_book_cart_like where deleted = 0 and book_id in (select book_id from t_book where user_id=#{userId} and deleted = 0) ")
    List<BookCartLike> getListByIssureId(int userId);

    @Select("select count(*) from t_book_cart_like where cl_status in (1,3) and book_id in (select book_id from t_book where user_id=#{userId}) and deleted = 0")
    int getLikeNumByIssureId(int userId);

    @Select("select book.*,cl.cart_num,truncate(cl.cart_num*book.n_price*book.discount/10,2) as countMoney,truncate(book.n_price*book.discount/10,2) as price,cl.cl_id clId from t_book_cart_like cl join t_book book on book.book_id = cl.book_id where cl_status in (2,3) and cl.user_id=#{userId} and cl.deleted = 0")
    List<CartInfo> getCartByPageByUserId(IPage<Book> page, QueryWrapper<Book> wrapper, int i, Integer userId);

    @Select("select count(*) from t_book_cart_like where cl_status in (2,3) and user_id=#{userId} and deleted = 0")
    int getCartTotal(Integer userId);

    @Select("select * from t_book_cart_like where cl_id=#{cartId}")
    BookCartLike getOneById(int cartId);

    @Update("update t_book_cart_like set deleted = 1 where cl_id=#{cartId} ")
    int deleteByClId(int cartId);

    @Update("update t_book_cart_like set cl_status = 1,cart_num=0 where cl_id=#{cartId} ")
    int cart2like(int cartId);

    @Select("select * from t_book_cart_like where book_id=#{bookId} and user_id=#{userId} and deleted = 0")
    BookCartLike getByBookId(BookCartLike cart);

    @Update("update t_book_cart_like set cl_status = 3,cart_num=#{cartNum} where cl_id=#{clId} ")
    int like2all(BookCartLike bookCartLike);

    @Update("update t_book_cart_like set cl_status = 3 where cl_id=#{clId} ")
    int cart2all(Integer clId);

    @Update("update t_book_cart_like set cl_status=#{clStatus} where cl_id=#{clId} ")
    int changeStatus(BookCartLike bookCartLike);

    @Update("update t_book_cart_like set cl_status=#{clStatus},cart_num=cart_num+#{cartNum} where cl_id=#{clId} ")
    int addCartNum(BookCartLike model);

    @Update("update t_book_cart_like set cart_num=cart_num+#{cartNum} where cl_id=#{clId} ")
    int OaddCartNum(BookCartLike model);

    @Update("update t_book_cart_like set cart_num=cart_num-#{cartNum} where cl_id=#{clId} ")
    int reduceCartNum(BookCartLike bookCartLike);
}
